package com.mbiolance.cloud.activiti.restful;

import cn.hutool.core.io.IoUtil;
import com.mbiolance.cloud.activiti.domain.dto.ActivitiFormDto;
import com.mbiolance.cloud.activiti.domain.dto.ProcessTaskDto;
import com.mbiolance.cloud.activiti.rpc.ProcessTaskFeignService;
import com.mbiolance.cloud.activiti.service.ProcessTaskService;
import com.mbiolance.cloud.auth.common.SystemRuntimeException;
import com.mbiolance.cloud.auth.domain.PageModel;
import com.mbiolance.cloud.auth.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@Api(tags = "流程任务相关接口")
@RestController
@Slf4j
@RequestMapping("process-task")
public class ProcessTaskRestful implements ProcessTaskFeignService {

    @Autowired
    private ProcessTaskService processTaskService;


    @ApiOperation("获取流程任务的分页列表")
    @GetMapping("page")
    public R<PageModel<ProcessTaskDto>> page(@RequestParam(required = false, defaultValue = "1") Integer number
            , @RequestParam(required = false, defaultValue = "10") Integer size) {
        PageModel<ProcessTaskDto> page = processTaskService.page(number, size);
        return R.ok(page);
    }

    @Override
    public R<Void> completeTask(String taskId, Map<String, Object> variables) {
        processTaskService.completeTask(taskId, variables);
        return R.ok();
    }

    @ApiOperation("获取任务当前的流程图")
    @GetMapping("image/{taskId}")
    public void image(@PathVariable String taskId, HttpServletResponse response) {
        InputStream image = processTaskService.image(taskId);
        try {
            IoUtil.copy(image, response.getOutputStream(), IoUtil.DEFAULT_BUFFER_SIZE);
        } catch (Exception e) {
            throw new SystemRuntimeException("文件下载失败，失败原因： " + e.getMessage());
        } finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                log.error("文件流关闭失败，失败原因： " + e.getMessage());
            }
        }
    }

    @Override
    public R<PageModel<ProcessTaskDto>> roleTodo(Integer number, Integer size) {
        PageModel<ProcessTaskDto> todo = processTaskService.roleTodo(number, size);
        return R.ok(todo);
    }

    @Override
    public R<PageModel<ProcessTaskDto>> todo(Integer number, Integer size) {
        PageModel<ProcessTaskDto> todo = processTaskService.todo(number, size);
        return R.ok(todo);
    }

    @Override
    public R<PageModel<ProcessTaskDto>> done(Integer number, Integer size) {
        PageModel<ProcessTaskDto> done = processTaskService.done(number, size);
        return R.ok(done);
    }

    @Override
    public R<ActivitiFormDto> getForm(String taskId) {
        ActivitiFormDto form = processTaskService.getForm(taskId);
        return R.ok(form);
    }

    @Override
    public R<Boolean> claim(String id) {
        return R.ok(processTaskService.claim(id));
    }

    @Override
    public R<Boolean> retTask(String id) {
        return R.ok(processTaskService.retTask(id));
    }
}
